package GetLoaction;

import java.util.ArrayList;

import pub.ConfigFile;
import udpwork.Package;
import udpwork.TransmitPack;
import udpwork.TransmitPack.TwoCommand;
import android.app.Application;
import android.os.Process;
import android.os.Vibrator;
import android.util.Log;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.BDNotifyListener;
import com.baidu.location.LocationClient;

import file.FileReadWrite;

public class Location extends Application {

	public LocationClient mLocationClient = null;
	//public GeofenceClient mGeofenceClient;
	private String mData;  
	public MyLocationListenner myListener = new MyLocationListenner();
	public static String TAG = "LocTestDemo";
	
	@Override
	public void onCreate() {
		mLocationClient = new LocationClient( this );
		/**鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�
		 * 杩欓噷鐨凙K鍜屽簲鐢ㄧ鍚嶅寘鍚嶇粦瀹氾紝濡傛灉浣跨敤鍦ㄨ嚜宸辩殑宸ョ▼涓渶瑕佹浛鎹负鑷繁鐢宠鐨凨ey
		 * 鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�鈥斺�
		 */
		//mLocationClient.setAK("D0565f1172f038a2a3e4478310197bf8");
		mLocationClient.registerLocationListener( myListener );
		//mGeofenceClient = new GeofenceClient(this);
		//浣嶇疆鎻愰啋鐩稿叧浠ｇ爜
//		mNotifyer = new NotifyLister();
//		mNotifyer.SetNotifyLocation(40.047883,116.312564,3000,"gps");//4涓弬鏁颁唬琛ㄨ浣嶇疆鎻愰啋鐨勭偣鐨勫潗鏍囷紝鍏蜂綋鍚箟渚濇涓猴細绾害锛岀粡搴︼紝璺濈鑼冨洿锛屽潗鏍囩郴绫诲瀷(gcj02,gps,bd09,bd09ll)
//		mLocationClient.registerNotify(mNotifyer);
		
		super.onCreate(); 
		Log.d(TAG, "... Application onCreate... pid=" + Process.myPid());
	}

	
	/**
	 * 鐩戝惉鍑芥暟锛屾湁鏇存柊浣嶇疆鐨勬椂鍊欙紝鏍煎紡鍖栨垚瀛楃涓诧紝杈撳嚭鍒板睆骞曚腑
	 */
	public class MyLocationListenner implements BDLocationListener {
		public void onReceiveLocation(BDLocation location) {
			if (location == null)
				return ;
			StringBuffer sb = new StringBuffer(256);
			sb.append("time : ");
			sb.append(location.getTime());
			sb.append("\nerror code : ");
			sb.append(location.getLocType());
			sb.append("\nlatitude : ");
			sb.append(location.getLatitude());
			sb.append("\nlontitude : ");
			sb.append(location.getLongitude());
			sb.append("\nradius : ");
			sb.append(location.getRadius());
			if (location.getLocType() == BDLocation.TypeGpsLocation){
				sb.append("\nspeed : ");
				sb.append(location.getSpeed());
				sb.append("\nsatellite : ");
				sb.append(location.getSatelliteNumber());
			} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
				/**
				 * 格式化显示地址信息
				 */
//				sb.append("\n省：");
//				sb.append(location.getProvince());
//				sb.append("\n市：");
//				sb.append(location.getCity());
//				sb.append("\n区/县：");
//				sb.append(location.getDistrict());
				sb.append("\naddr : ");
				sb.append(location.getAddrStr());
			}
			sb.append("\nsdk version : ");
			sb.append(mLocationClient.getVersion());
			sb.append("\nisCellChangeFlag : ");
			sb.append(location.isCellChangeFlag());
			//logMsg(sb.toString());
			//Log.i(TAG, sb.toString());
			//System.out.println( sb.toString());
			toMsg(location.getTime(),location.getLatitude(),location.getLongitude(),location.getAddrStr());
		}
		
		public void onReceivePoi(BDLocation poiLocation) {
			if (poiLocation == null){
				return ; 
			}
			StringBuffer sb = new StringBuffer(256);
			sb.append("Poi time : ");
			sb.append(poiLocation.getTime());
			sb.append("\nerror code : "); 
			sb.append(poiLocation.getLocType());
			sb.append("\nlatitude : ");
			sb.append(poiLocation.getLatitude());
			sb.append("\nlontitude : ");
			sb.append(poiLocation.getLongitude());
			sb.append("\nradius : ");
			sb.append(poiLocation.getRadius());
			if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation){
				sb.append("\naddr : ");
				sb.append(poiLocation.getAddrStr());
			} 
			if(poiLocation.hasPoi()){
				sb.append("\nPoi:");
				sb.append(poiLocation.getPoi());
			}else{				
				sb.append("noPoi information");
			}
			//logMsg(sb.toString());
			
			//System.out.println( sb.toString());
			toMsg(poiLocation.getTime(),poiLocation.getLongitude(),poiLocation.getLatitude(),poiLocation.getAddrStr());
		}
	}
	
	private void toMsg(String time, double Jd, double Wd, String addrs){
		if (ConfigFile.UserName ==null) return;
		ConfigFile.MyLocation.Buff ="时间:"+time+";经度:"+Jd+";维度:"+Wd+";地址:"+addrs;
		//ConfigFile.MyLocation.Buff ="时间:"+time+";经度:"+ConfigFile.MyLocation.dJd+";维度:"+ConfigFile.MyLocation.dWd+";地址:"+addrs;
		if (addrs==null) return;
		if(500<distance(Jd,Wd,ConfigFile.MyLocation.Jd,ConfigFile.MyLocation.Wd))  //表示移动了，需要记录当前位置
		{
			ConfigFile.MyLocation.Jd=Jd;
			ConfigFile.MyLocation.Wd=Wd;
			
			String fname="Loaction" + ".Loa";
		    FileReadWrite frw= new FileReadWrite(this, fname);
		    frw.WriteText( ConfigFile.MyLocation.Buff  + "\n");			
		}
		
		if (2000<distance(Jd,Wd,ConfigFile.MyLocation.dJd,ConfigFile.MyLocation.dWd))  //判断是否离开家了
		{
			
			if (2!=ConfigFile.MyLocation.isSendSms) {
				sendSms("离开家! " + ConfigFile.MyLocation.Buff ,ConfigFile.ServerPhone);
				ConfigFile.MyLocation.isSendSms=2;
			}
		} else {   //回到家了
			if (1!=ConfigFile.MyLocation.isSendSms) {
				sendSms("回到家! " + ConfigFile.MyLocation.Buff ,ConfigFile.ServerPhone);
				ConfigFile.MyLocation.isSendSms=1;
			}
		}
	}
	
	public double distance(double n1, double e1, double n2, double e2)
    {
        double jl_jd = 102834.74258026089786013677476285;
        double jl_wd = 111712.69150641055729984301412873;
        double b = Math.abs((e1 - e2) * jl_jd);
        double a = Math.abs((n1 - n2) * jl_wd);
        return Math.sqrt((a * a + b * b));
    }
	
	private void sendSms(String str,String phoneNum){
		ArrayList<String> targ = new ArrayList<String>();
		Package pack = new Package();
		String date = ConfigFile.LongToDateString(System.currentTimeMillis());
		targ.add(date); // 发送消息时间
		targ.add(str); // 发送消息内容
		targ.add("9999"); // 发送消息的ID
		TransmitPack tp = new TransmitPack(TwoCommand.GetSms, targ);
		pack.TransmitToClient(tp, phoneNum); // 组包，并发送
	}
}